package com.rao.quartz.controller;

import com.rao.domain.inputPojo.userInfoInput;
import com.rao.entity.User;
import com.rao.quartz.service.serviceInterface.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: raoguang
 * @Date: 2022/05/25/22:54
 * @Description:
 */
@RestController
public class userController {

    @Autowired
    private UserService userService;

    @GetMapping("/getAllUserInfo")
    public List<User> getAllUserInfo() {
        return userService.findAllUserInfo();
    }

    @GetMapping("/getSingleUserInfo/{memId}")
    public User getSingleUserInfo(@PathVariable("memId") String userId) {
        return userService.findSingleUserInfo(userId);
    }

    @PostMapping("/addUserInfo")
    public String addUserInfo(@RequestBody User user) {
        boolean ifSuccess = false;
        if (user != null) {
            ifSuccess = userService.insertUserInfo(user);
        } else {
            return "用户对象不能为空";
        }
        if (ifSuccess) {
            return user.toString();
        }
        return "插入失败";
    }

    @PostMapping("/updateUserInfo/{userId}")
    public String updateUserInfo(@RequestBody userInfoInput userInfo, @PathVariable int userId) {
        boolean ifSuccess = false;
        if (!StringUtils.isEmpty(userId)) {
            ifSuccess = userService.updateUserInfo(userId, userInfo.user);
        } else {
            return "传入的用户Id不能为空";
        }
        if (ifSuccess) {
            return userInfo.user.toString();
        }
        return "修改失败";
    }

    @GetMapping("/deleteUser/{memId}")
    public String deleteUser(@PathVariable("memId") int userId) {
        boolean ifSuccess = false;
        if (!StringUtils.isEmpty(userId)) {
            ifSuccess = userService.deleteUserInfo(userId);
        } else {
            return "传入的用户Id不能为空";
        }
        if (ifSuccess) {
            return "删除成功";
        }
        return "删除失败";
    }


}
